//
//  PlayDetailSkinViewController.swift
//  TimMusic
//
//  Created by nkl on 2024/10/12.
//

import CollectionViewPagingLayout
import Foundation

class PlayDetailSkinViewController: BasicViewController, UICollectionViewDataSource {
    lazy var backButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "ic_back"), for: .normal)
        btn.addTarget(self, action: #selector(dismissCurrentVc), for: .touchUpInside)
        return btn
    }()

    var collectionView: UICollectionView!

    var skins: [String] = ["skin_0", "skin_1", "skin_2"]

    private func setupCollectionView() {
        let layout = CollectionViewPagingLayout()
        layout.delegate = self
        collectionView = UICollectionView(frame: view.frame, collectionViewLayout: layout)
        collectionView.backgroundColor = .clear
        collectionView.isPagingEnabled = true
        collectionView.register(PlayDetailSkinCell.self, forCellWithReuseIdentifier: "PlayDetailSkinCell")
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.dataSource = self
        view.addSubview(collectionView)
    }

    lazy var pageControlView: PageControlView = {
        let page = PageControlView()
        page.numberOfPages = 3
        page.preferences = .init(currentColor: .white, color: .hexColor("#444444"), dimFactor: 0.4, dotWidth: 32, dotHeight: 2, gapSize: 6, currentDotBorderWidth: 0)
        return page
    }()

    lazy var saveButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("Save", for: .normal)
        btn.titleLabel?.font = .systemFont18
        btn.setTitleColor(UIColor.hexColor("#3D3D3D"), for: .normal)
        btn.backgroundColor = .white
        btn.layer.cornerRadius = 25
        btn.layer.masksToBounds = true
        return btn
    }()

    override func viewDidLoad() {
        setupCollectionView()
        super.viewDidLoad()
        view.backgroundColor = .hexColor("#232323")
    }

    @objc func dismissCurrentVc() {
        dismiss(animated: true)
    }

    override func addChildren() {
        super.addChildren()
        view.addSubview(backButton)
        view.addSubview(collectionView)
        view.addSubview(pageControlView)
        view.addSubview(saveButton)
    }

    override func makeConstraints() {
        super.makeConstraints()
        backButton.snp.makeConstraints { make in
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.width.height.equalTo(24)
            make.leading.equalToSuperview().offset(16)
        }

        collectionView.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.bottom.equalTo(saveButton.snp.top).offset(-80)
            make.top.equalTo(backButton.snp.bottom)
        }

        pageControlView.snp.makeConstraints { make in
            make.top.equalTo(collectionView.snp.bottom)
            make.centerX.equalToSuperview()
        }

        saveButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
            make.width.equalTo(250)
            make.height.equalTo(50)
        }
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        skins.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PlayDetailSkinCell", for: indexPath) as! PlayDetailSkinCell
        if indexPath.row < skins.count {
            cell.card.image = UIImage(named: skins[indexPath.row])
        }
        return cell
    }
}

extension PlayDetailSkinViewController: CollectionViewPagingLayoutDelegate {
    func onCurrentPageChanged(layout: CollectionViewPagingLayout, currentPage: Int) {
        pageControlView.currentPage = currentPage
    }
}
